<#assign pageName="用户权限分配">
<@override name="head-styles">
    <style>
        .checkbox{
            padding:0;
        }
    </style>
</@override>
<@override name="page-content">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>${pageName}</legend>
    </fieldset>
    <form class="layui-form padding-10" lay-filter="formUserAuth">
        <@ctrl.Hidden path="users.userId" />
        <@ctrl.Textbox path="users.uname" labelText="用户名称" required="true" attributes='readonly'/>
        <@ctrl.TreeSelect path="users.guid" dataList=guidsList![] fieldText="sysModuleName" fieldValue="guid" labelText="所属平台" 
            fieldParentID="upperGuid" parentidValue="${(guids.upperGuid)!'-1'}" required="true"/>
        <@ctrl.TreeSelect path="users.depId" dataList=depList![] fieldText="depName" fieldValue="depId" labelText="所属机构" 
            fieldParentID="upperId" parentidValue="${(dep.upperId)!'-1'}" required="true"/>
        <@ctrl.CheckboxList path="users.roleId" options=roleList![] labelText="角色权限" textField="roleName" valueField="roleId" required="required" />
        <@ctrl.FormAction backURL="../management"/>
    </form>
</@override>
<@override name="page-scripts">
    <script>
        $(function(){
            layui.use(['form','layer', 'jquery'], function(){
                var form = layui.form;
                var layer=layui.layer;
                $=layui.jquery;
                //监听提交
                form.on('submit(submit)', function(data){
                    var checkedRole =[]; 
                    var checkedGuid =[]; 
                    $('input[name="roleId"]:checked').each(function(index, item){ 
                        checkedRole.push($(this).val()); 
                    });
                    data.field.roleId = checkedRole;
                    submitData(data.field, '${contextPath}/admin/users/auth', function(){location.href="${contextPath}/admin/users/management";});
                    return false;
                });
                //平台机构角色联动
                form.on('select(guid)', function(data){
                    var guid = data.value;
                    $.post('${rc.getContextPath()}/admin/department/queryall', {'guid': guid}, function(data){
                        var $html = "<option value=''>－请选择所属机构－</option>"
                        $.each(data, function(index, item){
                            $html += "<option value='" + item.depId + "'>" + item.depName + "</option>";
                        })
                        $("#depId").html($html);
                        form.render("select");
                    })
                });
                form.on('select(depId)', function(data){
                    var $this = $(this);
                    var guid = $('#guid').val();
                    var userId = $('#userId').val();
                    var depId = data.value;
                    $.post('${rc.getContextPath()}/admin/roles/queryrole', {'guid':guid,'depId':depId,'userId':userId}, function(data){
                        var $html = '';
                        $.each(data, function(index, item){
                            var checked= "";
                            if(!!item["checked"]){
                                checked = "checked";
                            }
                            $html += '<input type="checkbox" name="roleId"  title="'+item['roleName']+'" value="'+item['roleId']+'" '+checked+' lay-skin="primary">';
                        })
                        if($html==""){
                            $html= '<div style="color:red;line-height: 38px; font-weight: bold;">当前机构下暂无角色，请先到角色管理中添加！</div>';
                        }
                        $this.parents(".layui-form-item").next().find('.layui-input-block').html($html);
                        form.render();
                    })
                });
            })
       })
    </script>
</@override>
<@extends name="/layout/basepage.html"/> 
